package com.shujia.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;

public class Code02DataOperator {
    FileSystem fileSystem;

    @Before
    public void getCon1(){
        URI uri = null;// hdfs 表示连接的协议 同时也表明了连接的目标类型
        try {
            uri = new URI("hdfs://master:9000");
            Configuration entries = new Configuration(); // 配置类
//            String user = "root";
            System.setProperty("HADOOP_USER_NAME","root"); // 通过Java代码的方式设置当前系统的环境变量
            fileSystem = FileSystem.get(uri, entries);
//            fileSystem = FileSystem.get(uri, entries,user);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    // 1.上传数据
    @Test
    public void putData(){

        Path src = new Path("./data/hdfs/Api.txt"); // 本地路径
        Path dst = new Path("/");
        try {
            fileSystem.copyFromLocalFile(src,dst);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void getData(){
        Path dfsPath = new Path("/Api.txt");
        Path localPath = new Path("./data/hdfs/Api2.txt");

        /*
            java.io.FileNotFoundException: java.io.FileNotFoundException: HADOOP_HOME and hadoop.home.dir are unset.
            由于HDFS写数据到本地，那么需要在当前环境中设置Hadoop的配置环境，执行相对应的命令进行拉去数据
                所以需要配置可执行环境
                下载Hadoop对应win运行环境下的 配置包 解压后配置 HADOOP_HOME 添加到PATH路径 重启IDEA
                    测试： terminal中输入 winutils

            Could not locate Hadoop executable: D:\software\hadoop-3.1.3\bin\winutils.exe
                需要 winutils
         */

        try {
            fileSystem.copyToLocalFile(dfsPath,localPath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void mkdir(){
        try {
            fileSystem.mkdirs(new Path("/api/test1/test2"));
//            fileSystem.mkdirs(new Path("/api"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void deleteFile(){
        try {
            fileSystem.delete(new Path("/api"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Test
    public void listStatus(){
        try {
            // fileStatuses当前路径下所有目录及文件的描述信息
            FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/api"));

            for (FileStatus fileStatus : fileStatuses) {
                System.out.println("path:"+fileStatus.getPath());
                System.out.println("owner:"+fileStatus.getOwner());
                System.out.println("owner:"+fileStatus.getReplication());

                if (fileStatus.isFile()) {
                    BlockLocation[] fileBlockLocations = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
                    System.out.println("Block location:"+Arrays.toString(fileBlockLocations));
                }
            }


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }





    @After
    public void close(){
        try {
            fileSystem.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
